package com.abs.orm.db.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.abs.orm.db.jdbc.provider.ConnectionProvider;

public class ConnectionManager {

	private ConnectionManager() {
		super();
	}

	// public static void main(String[] args) {
	// ConnectionManager.getConnection();
	// ConnectionManager.close();
	// ConnectionManager.getConnection(JdbcType.Oracle);
	// ConnectionManager.close(JdbcType.Oracle);
	// System.out.print("TEST");
	// }

	public static Connection getConnection() {
		return ConnectionProvider.getInstance(JdbcType.MySQL).getConnection();
	}

	public static Connection getConnection(JdbcType jdbcType) {
		return ConnectionProvider.getInstance(jdbcType).getConnection();
	}

	public static void close() {
		ConnectionProvider.getInstance(JdbcType.MySQL).close();
	}

	public static void close(JdbcType jdbcType) {
		ConnectionProvider.getInstance(jdbcType).close();
	}

	public static void close(Connection arg0) {
		if (arg0 != null) {
			try {
				if (!arg0.isClosed()) {
					arg0.close();
					arg0 = null;
				} else {
					arg0 = null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				arg0 = null;
			}
		}
	}

	public static void close(ResultSet arg0) {
		if (arg0 != null) {
			try {
				// if (!arg0.isClosed()) {
				arg0.close();
				arg0 = null;
				// } else {
				// arg0 = null;
				// }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				arg0 = null;
			}
		}
	}

	public static void close(Statement arg0) {
		if (arg0 != null) {
			try {
				// if (!arg0.isClosed()) {
				arg0.close();
				arg0 = null;
				// } else {
				// arg0 = null;
				// }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				arg0 = null;
			}
		}
	}

	public static void close(PreparedStatement arg0) {
		if (arg0 != null) {
			try {
				// if (!arg0.isClosed()) {
				arg0.close();
				arg0 = null;
				// } else {
				// arg0 = null;
				// }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				arg0 = null;
			}
		}
	}
}
